package com.example.victory.service.sync;

import com.example.victory.entity.companyEntity.DeptAccountEntity;
import com.example.victory.entity.companyEntity.TxtjyrEntity;
import com.example.victory.mapper.DeptAndAccountMapper;
import com.example.victory.mysql.DeptAndAccountMysqlMapper;
import com.cgws.fintech.sdk.common.lock.Synchronized;
import com.example.victory.utils.DateHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SyncTxtjyrService {
    @Autowired
    private DeptAndAccountMapper deptAndAccountMapper;

    @Autowired
    private DeptAndAccountMysqlMapper deptAndAccountMysqlMapper;

    @Synchronized(namespace = "inner-business-customer:syncTxtjyr", expireSecond = 600)
    @Transactional
    @Scheduled(cron = "0 0 2 * * ?")
    public void SyncAccountInfo() {
        log.info(String.format("开始同步交易日数据"));
        if(StringUtils.isEmpty(deptAndAccountMysqlMapper.getNowTime(DateHelper.getDateYYYYMMDD(new Date())))){
            log.info(String.format("交易日有数据,结束同步交易日数据"));
            return;
        }

        List<TxtjyrEntity> deptList=  deptAndAccountMapper.findTxtjyr();
        if(deptList==null||deptList.size()==0){
            return;
        }
        deptAndAccountMysqlMapper.truncateTxtjyr();
        deptAndAccountMysqlMapper.insertTxtjyrBatch(deptList);
        log.info(String.format("结束同步交易日数据"));
    }


}
